<template>
	<view class="delay-time">
		<view class="text-box">项目名称：{{orderDetail.productName}}</view>
		<view class="text-box">房间类型：{{dealProductType}}</view>
		<view class="text-box">预定到期时间：{{time}}</view>
		<view class="flex-box">
			<view class="text-box">选择到期时间：</view>
		</view>
		<view>
			<uni-datetime-picker type="datetime" :start="nowDateTime" returnType="date" v-model="delayEndTime"
				@change="changeLog" />
		</view>
		<view class="time-box">
			<timeLineBar :bookingTimeVo="bookingTimeVo"></timeLineBar>
		</view>
		<view class="btn-box">
			<view class="submit-btn" @click="submitDelay">确认</view>
		</view>
	</view>
</template>

<script>
	import dayjs from 'dayjs'
	import timeLineBar from '@/components/time-line-bar.vue'
	export default {
		components: {
			timeLineBar
		},
		computed: {
			dealProductType() {
				let type = ''
				this.productionModel.forEach((item, index) => {
					if (item.modelType === this.orderDetail.productType) {
						type = item.modelName
					}
				})
				return type
			}
		},
		data() {
			return {
				productionModel: [],
				currentPackage: -1,
				packageArr: [],
				bookingTimeVo: {
					hours: [],
					timeSlotList: []
				},
				hour: 0,
				time: '',
				endTime: '',
				orderId: '',
				orderDetail: {},
				delayEndTime: '',
				nowDateTime: '',
				isOrder: false
			}
		},
		methods: {
			changeLog(e) {
				this.delayEndTime = dayjs(e).format('YYYY-MM-DD HH:mm:ss')
			},
			// 获取延时详情
			getDelayDetail() {
				this.$request({
					method: 'post',
					url: `/applet/store/order/delayInfo/${this.orderId}`,
					loading: true,
					isEncrypt: false
				}).then(res => {
					const {
						bookingTimeVo,
						orderDetail
					} = res.data
					this.bookingTimeVo = bookingTimeVo
					this.orderDetail = orderDetail
				}).catch(err => {
					console.log('请求异常：', err)
				})
			},
			// 延时
			submitDelay() {
				const data = {
					orderId: this.orderId,
					newEndTime: this.delayEndTime
				}
				this.$request({
					method: 'post',
					url: `/applet/store/order/delay`,
					data,
					loading: true,
					isEncrypt: false
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: `订单结束时间修改为${this.delayEndTime}`,
							duration: 2000,
							icon: 'none'
						})
						setTimeout(() => {
							if (this.isOrder) {
								const eventChannel = this.getOpenerEventChannel()
								eventChannel.emit('reload')
							}
							uni.navigateBack()
						}, 2000)
					} else {
						uni.showToast({
							title: res.msg,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log('请求异常：', err)
				})
			},
			// 获取产品类型
			getProductionModel() {
				this.$request({
					method: 'get',
					url: `/applet/store/product/modelList`,
					loading: true,
					isEncrypt: false
				}).then(res => {
					this.productionModel = res.data || []
				}).catch(err => {
					console.log('查询异常：', err)
				})
			},
		},
		onLoad(option) {
			this.orderId = option.orderId
			this.time = option.time
			this.delayEndTime = option.time
			if (option?.isOrder === 'true') {
				this.isOrder = true
			}
			this.getDelayDetail()
			this.nowDateTime = dayjs(new Date().getTime()).format('YYYY-MM-DD HH:mm:ss')
			this.getProductionModel()
		}
	}
</script>

<style lang="scss">
	.delay-time {
		padding: 30rpx;
		background: #FFF;
		height: calc(100vh - 60rpx);

		.text-box {
			font-size: 28rpx;
			margin-bottom: 40rpx;
		}

		.flex-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.time-box {
			background: #FFF;
			padding: 30rpx 20rpx;
		}

		.package-box {
			display: flex;
			flex-wrap: wrap;
			padding: 0 12rpx;
			margin-bottom: 40rpx;

			&-item {
				flex: 0 0 106rpx;
				margin: 0 6rpx 20rpx 6rpx;
				text-align: center;
				font-size: 26rpx;
				color: #333;
				font-weight: bold;
				border: 1px solid #999;
				border-radius: 12rpx;
				padding: 26rpx 16rpx;
				overflow: hidden;
			}

			.act-btn {
				border: 1px solid #f54d54;
				background: #fff9e3;
				position: relative;
				color: #f54d54;
			}

			.act-btn::after {
				content: '√';
				color: #FFF;
				text-align: center;
				position: absolute;
				width: 26rpx;
				height: 26rpx;
				background: #f54d54;
				bottom: 0;
				right: 0;
				border-radius: 10rpx 0 0 0;
			}
		}

		.no-data {
			padding-bottom: 40rpx;
			text-align: center;
			color: #CCC;
			font-size: 32rpx;
		}

		.btn-box {
			position: fixed;
			bottom: 0;
			padding: 30rpx 0;
			box-sizing: border-box;
			width: calc(100% - 60rpx);

			.submit-btn {
				height: 100rpx;
				color: #FFF;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 10rpx;
				background: #ff9600;
				flex: 1;
			}
		}
	}
</style>